var express = require("express");
var uunewid = require("uuid");
var router = express.Router();
const { checkAuth } = require("../utils/utils");
var constants = require("../utils/constants");
let log = require("cf-nodejs-logging-support");
log.setLoggingLevel("info");
const { ohana } = require("ohana-node-orm");
function checkData(res, data) {
  if (data == null) {
    res.send(500);
  }
}

const tableName = "SAP_JIANGDU_POLICYS_HELP";



router.get("/", function (req, res, next) {
  // #swagger.tags = ['政策小助手']
  // #swagger.summary = '获取政策小助手'
  /* #swagger.security = [{
               "JiangduJWT": []
  }] */
  /* #swagger.parameters['POLICY_ID'] = {
    description: "String",
    required: false,
} */
  const user = checkAuth(req.user, constants.BASICAUTH);
  if (!user.authCheck) {
    res.sendStatus(403);
    return;
  }
  let { POLICY_ID } = req.query;
  console.log(POLICY_ID)
  if (POLICY_ID == undefined) {
    POLICY_ID = {};
  } else {
    POLICY_ID = { POLICY_ID: POLICY_ID }
  }
  getData(POLICY_ID)
    .then((result) => {
      res.send(result);
    })
    .catch((err) => {
      log.error(err);
      res.status(500).send(err);
    });
});

async function getData(data) {

  const user = new ohana(tableName);
  try {
    const result = await user.find(data);
    return result;
  } catch (error) {
    log.error(error);
    return -1;
  }

}
router.post("/", function (req, res, next) {
  // #swagger.tags = ['政策小助手']
  // #swagger.summary = '创建政策小助手'
  /* #swagger.security = [{
               "JiangduJWT": []
  }] */
  /*	#swagger.requestBody = {
              required: true,
              content: {
                  "application/json": {
                      schema: {
                          $ref: "#/definitions/policyhelp_add"
                      }  
                  
                  
                  
                    },
              }
      } */
  const { data } = req.body;
  const user = checkAuth(req.user, constants.FINAUTH);
  if (!user.authCheck) {
    res.sendStatus(403);
    return;
  }
  data.ID = uunewid.v4();
  insertData(data)
    .then((result) => {
      if (result == 1) {
        res.sendStatus(200);
      } else {
        res.send(400);
      }
    })
    .catch((err) => {
      log.error(err);
      res.status(500).send(err);
    });
});

async function insertData(body) {
  const user = new ohana(tableName);
  try {
    const result = await user.insert(body);
    return result;
  } catch (error) {
    log.error(error);
    return -1;
  }
}

/*delete something*/
router.delete("/", function (req, res, next) {
  // #swagger.tags = ['政策小助手']
  // #swagger.summary = '删除政策小助手'
  /* #swagger.security = [{
               "JiangduJWT": []
  }] */
  /*	#swagger.requestBody = {
              required: true,
              content: {
                  "application/json": {
                      schema: {
                          $ref: "#/definitions/fin_delete"
                      }  
                  },
              }
      } */
  checkData(res, req.body.data);
  const { data } = req.body;
  deleteFin(data)
    .then((result) => {
      if (result == 1) {
        res.sendStatus(200);
      } else {
        res.send(400);
      }
    })
    .catch((err) => {
      log.error(err);
      res.status(500).send(err);
    });
});
/// fin tags
module.exports = router;
